package com.jxzyk.model;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.core.adapter.FragmentPagerAdapter;
import com.core.listener.I_PageChange;
import com.core.ui.activity.BaseMainActivity;
import com.core.ui.bar.footbar.FootBar;
import com.core.ui.bar.footbar.FootBar.OnFootClickListener;
import com.core.ui.bar.widget.Menu;
import com.jxzyk.R;
import com.jxzyk.model.center.CenterFragment;
import com.jxzyk.model.course.CourseFragment;
import com.jxzyk.model.found.FoundFragment;
import com.jxzyk.model.know.KnowFragment;
import com.jxzyk.util.FragmentPager;
import com.jxzyk.view.FootItem;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.act_main)
public class MainActivity extends BaseMainActivity {

	@ViewInject(R.id.footBar)
	FootBar footBar;
	@ViewInject(R.id.viewPager)
	public ViewPager viewPager;
	public Fragment foundFragment, courseFragment, knowFragment,
			centerFragment;

	@Override
	public void initData(Bundle bundle) {

	}

	@Override
	public void initUi() {
		initViewpager();
		initFootBar();
	}

	private void initViewpager() {
		List<Fragment> fragments = new ArrayList<Fragment>();
		foundFragment = new FoundFragment();
		courseFragment = new CourseFragment();
		knowFragment = new KnowFragment();
		centerFragment = new CenterFragment();
		fragments.add(foundFragment);
		fragments.add(courseFragment);
		fragments.add(knowFragment);
		fragments.add(centerFragment);
		FragmentPagerAdapter adapter = new FragmentPagerAdapter(
				getSupportFragmentManager(), viewPager, fragments);
		viewPager.setAdapter(adapter);
		adapter.setOnExtraPageChangeListener(new I_PageChange() {

			@Override
			public void onExtraPageSelected(int i) {
				footBar.switchStyle(i);
			}

			@Override
			public void onExtraPageScrolled(int i, float v, int i2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onExtraPageScrollStateChanged(int i) {
				// TODO Auto-generated method stub

			}
		});

		// pager = new FragmentPager(getSupportFragmentManager(),
		// R.id.tab_content, fragments);
	}

	// public FragmentPager pager;
	private static int[] titles = new int[] { R.string.main_tab_1,
			R.string.main_tab_2, R.string.main_tab_3, R.string.main_tab_4 };

	private void initFootBar() {
		Menu menu = new Menu(this);
		FootItem footItem = new FootItem(this);
		footItem.setTitle(titles[0]);
		footItem.setColor(new int[] { R.color.blue, android.R.color.black });
		footItem.setIcons(new int[] { R.drawable.ic_found, R.drawable.ic_found_ });
		menu.addMenuItem(footItem);

		footItem = new FootItem(this);
		footItem.setTitle(titles[1]);
		footItem.setColor(new int[] { R.color.blue, android.R.color.black });
		footItem.setIcons(new int[] { R.drawable.ic_course,
				R.drawable.ic_course_ });
		menu.addMenuItem(footItem);

		footItem = new FootItem(this);
		footItem.setTitle(titles[2]);
		footItem.setColor(new int[] { R.color.blue, android.R.color.black });
		footItem.setIcons(new int[] { R.drawable.ic_know, R.drawable.ic_know_ });
		menu.addMenuItem(footItem);

		footItem = new FootItem(this);
		footItem.setTitle(titles[3]);
		footItem.setColor(new int[] { R.color.blue, android.R.color.black });
		footItem.setIcons(new int[] { R.drawable.ic_center,
				R.drawable.ic_center_ });
		menu.addMenuItem(footItem);
		footBar.setBackgroundColor(Color.parseColor("#EEEEEE"));
		footBar.addMenu(menu, 0);
		footBar.setOnFootClickListener(new OnFootClickListener() {

			@Override
			public void onItemClick(FootBar view, Menu menu, int index) {
				// pager.moveToIndex(index);
				viewPager.setCurrentItem(index);
				// mToolbar.setTitle(getResources().getString(titles[index]));
			}
		});
	}
}
